import pyglet
import entity

from debug import msg

class Portalable(object):
    """An object that can pass through portals."""
    image = None

    def __init__(self, mode, start_configurations,
                 fixed_rotation=True, e_type=None):
        self.objects = []
        self.magic = False

        self.mode = mode
        self.fixed_rotation = fixed_rotation
        self.e_type = e_type

        # We cannot apply forces in the contact listener's postsolve
        # function. We need to do this in order to negate the forward
        # motion of object that are linked to something in the
        # collision. We solve this by storing a list of functions to
        # apply to the portalable object in the next frame, before any
        # other updates.
        #self.impulse_correction_functions = []

        for conf in start_configurations:
            self.add_instance(conf, front_is_left=False)

        # for ((x, y), (vel_x, vel_y), rot) in start_configurations:
                # self.objects.append(
                #     entity.Dynamic(self.mode, self.image,
                #                    x=x, y=y,
                #                    vel_x=vel_x, vel_y=vel_y,
                #                    rot=rot, fixed_rotation=fixed_rotation,
                #                    e_type=e_type,
                #                    portalable=self))

    def add_instance(self, start_configuration, front_is_left=True):
        o = entity.Dynamic(self.mode, self.image,
                           start_configuration, 
                           front_is_left=front_is_left,
                           portalable=self)
        self.objects.append(o)
        return o

    # def apply_hive_mind_collision(self):
    #     for f in self.impulse_correction_functions:
    #         f(self)
    #
    #     self.impulse_correction_functions = []

    def update(self, dt):
        for o in self.objects:
            o.update(dt)
